#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "server.h"
#include "staff.h"
#include "model.h"

// 全局变量， 存储用户ID
char username[BUFSIZE] = {0};


// 登录
void login(int recvfd, int msg_type, msg_t msg)
{
    int res = 0;
    msg_t reply;
    if (msg_type == TYPE_ADMIN_LOGIN) 
    {
        sqlite3 *db = db_create();
        char sql[128] = {0};
        sprintf(sql, "select id from %s where name = '%s' and passwd = '%s'", T_STAFF, msg.staff_info.name, msg.staff_info.passwd);
        int staff_exists = check_number(db, sql);


        if(staff_exists)
        {
            // 给全局变量赋值
            strcpy(username, msg.staff_info.name);
            // 添加登陆历史记录
            char history_desc[BUFSIZE];
            sprintf(history_desc, "登陆");
            add_history(history_desc);
            printf("登陆添加历史记录\n");

            // 标记登陆成功
            res = 1;
        }
    }
    else if (msg_type == TYPE_USER_LOGIN)
    {
        res = 1;
    }
    else
    {
        res = 0;
    }

    if (res == 1) {
        reply.type = SERVER_OK;
        strcpy(reply.msg, "成功");
    } else {
        reply.type = SERVER_ERR;
        strcpy(reply.msg, "失败");
    }

    res = send(recvfd, &reply, sizeof(msg), 0);
    if (res < 0) {
        ERR("send error");
    }

    // 添加历史记录
    
}

// 根据用户名查询： 普通用户查询及管理员查询
void search()
{

}

// 修改： 普通用户修改及管理员修改
void modify(int recvfd, msg_t msg)
{   
    int id = msg.staff_info.no;
    sqlite3 *db = db_create();
    msg_t reply;
    int res;

    if (strcmp("name", msg.msg) == 0) 
    {
        printf("change name :%s id:%d\n", msg.staff_info.name, id);
        res = db_change_name(db, msg.staff_info.name, id);
    }

    if (strcmp("age", msg.msg) == 0) 
    {
        res = db_change_age(db, msg.staff_info.age, id);
    }

    if (strcmp("addr", msg.msg) == 0) 
    {
        res = db_change_addr(db, msg.staff_info.addr, id);
    }

    if (strcmp("phone", msg.msg) == 0) 
    {
        res = db_change_phone(db, msg.staff_info.phone, id);
    }

    if (strcmp("position", msg.msg) == 0) 
    {
        res = db_change_position(db, msg.staff_info.position, id);
    }

    if (strcmp("salary", msg.msg) == 0) 
    {
        res = db_change_salary(db, msg.staff_info.salary, id);
    }

    if (strcmp("join_at", msg.msg) == 0) 
    {
        res = db_change_join_at(db, msg.staff_info.join_at, id);
    }

    if (strcmp("level", msg.msg) == 0) 
    {
        res = db_change_level(db, msg.staff_info.level, id);
    }

    if (strcmp("passwd", msg.msg) == 0) 
    {
        res = db_change_passwd(db, msg.staff_info.passwd, id);
    }

    if (res == 0) 
    {
        printf("修改成功\n");
        reply.type = SERVER_OK;
        strcpy(reply.msg, "修改成功");

        // 添加修改历史记录
        char history_desc[BUFSIZE];
        sprintf(history_desc, "修改用户[%d]的[%s]", id, msg.msg);
        add_history(history_desc);
        printf("添加修改历史记录\n");

    } else {
        printf("修改失败\n");
        reply.type = SERVER_ERR;
        strcpy(reply.msg, "修改失败");
    }

    res = send(recvfd, &reply, sizeof(msg), 0);
    if (res < 0) {
        ERR("send error");
    }
}

// 添加： 管理员添加员工信息
void add(int recvfd, msg_t msg)
{
    msg_t reply;
    sqlite3 *db = db_create();
    int res = db_insert(db,msg.staff_info);

    if (res == 0) 
    {
        reply.type = SERVER_OK;
        strcpy(reply.msg, "添加成功");
    } else {
        reply.type = SERVER_ERR;
        strcpy(reply.msg, "添加失败");
    }

    res = send(recvfd, &reply, sizeof(msg), 0);
    if (res < 0) {
        ERR("send error");
    }

    printf("添加员工信息完成");

    // 添加修改历史记录
    char history_desc[BUFSIZE];
    sprintf(history_desc, "添加用户[%d]%s", msg.staff_info.no, msg.staff_info.name);
    add_history(history_desc);
    printf("添加修改历史记录\n");
}

// 删除： 管理员删除员工信息
void del(int recvfd, msg_t msg)
{
    int staff_no = msg.staff_info.no;
    int res;
    sqlite3 *db = db_create();
    char sql[128] = {0};
    sprintf(sql, "select id from %s where id = %d", T_STAFF, staff_no);
    int staff_exists = check_number(db, sql);

    if (staff_exists <= 0) 
    {
        memset(&msg, 0, sizeof(msg));
        msg.type = SERVER_ERR;
        strcpy(msg.msg, "用户不存在");
        res = send(recvfd, &msg, sizeof(msg), 0);
        if (res < 0) {
            ERR("send error");
        }
    }

    db_del(db, msg.staff_info.no, msg.staff_info.name);
    memset(&msg, 0, sizeof(msg));
    msg.type = SERVER_OK;
    strcpy(msg.msg, "删除成功");

    // 添加删除历史记录
    char history_desc[BUFSIZE];
    sprintf(history_desc, "删除用户[%d]", staff_no);
    add_history(history_desc);
    printf("添加删除历史记录\n");

    res = send(recvfd, &msg, sizeof(msg), 0);
    if (res < 0) {
        ERR("send error");
    }
}

// 管理员操作历史记录
void history(int recvfd)
{
    sqlite3 *db = db_create();
    int res;
    char sql[128] = {0};
    sprintf(sql, "select id from %s where 1", T_HISTORY);
    msg_t msg;
    hsg_t history;
    int history_num = check_number(db, sql);
    int i;

    for (i = 0; i < history_num; i++) 
    {
        memset(sql, 0, sizeof(sql));
        memset(&history, 0, sizeof(history));
        sprintf(sql, "select * from %s where 1 order by id ASC limit 1 offset %d ", T_HISTORY, i);
        msg.history = check_history(db, sql);
        printf("history: %s:%s:%s\n", msg.history.time, msg.history.name, msg.history.words);
        msg.type = SERVER_OK;
        strcpy(msg.msg, "成功");

        res = send(recvfd, &msg, sizeof(msg), 0);
        if (res < 0) {
            ERR("send error");
        }
    }

    memset(sql, 0, sizeof(sql));
    msg.type = SERVER_FINNISH;
    strcpy(msg.msg, "发送完成");

    res = send(recvfd, &msg, sizeof(msg), 0);
    if (res < 0) {
        ERR("send error");
    }
}

// 查询所有员工信息
void all(int recvfd)
{
    sqlite3 *db = db_create();
    int res;
    char sql[128] = {0};
    sprintf(sql, "select id from %s where 1", T_STAFF);
    msg_t msg;
    staff_info_t staff_info;
    int staff_num = check_number(db, sql);
    int i;

    for (i = 0; i < staff_num; i++) 
    {
        memset(sql, 0, sizeof(sql));
        memset(&staff_info, 0, sizeof(staff_info));
        sprintf(sql, "select * from %s where 1 order by id limit 1 offset %d ", T_STAFF, i);
        staff_info = db_check_name(db, sql);
        msg.staff_info = staff_info;
        msg.type = SERVER_OK;
        strcpy(msg.msg, "成功");

        res = send(recvfd, &msg, sizeof(msg), 0);
        if (res < 0) {
            ERR("send error");
        }
    }

    memset(sql, 0, sizeof(sql));
    msg.type = SERVER_FINNISH;
    strcpy(msg.msg, "发送完成");

    res = send(recvfd, &msg, sizeof(msg), 0);
    if (res < 0) {
        ERR("send error");
    }
}


// 根据工号搜索
void search_by_no(int recvfd, msg_t msg)
{
    printf("根据工号搜索");
    sqlite3 *db = db_create();
    int res;
    char sql[128] = {0};
    sprintf(sql, "select id from %s where id = %d", T_STAFF, msg.staff_info.no);
    int staff_num = check_number(db, sql);
    msg_t reply;

    if (staff_num > 0) 
    {
        reply.type = SERVER_OK;
        strcpy(reply.msg, "员工存在");
    }
    else 
    {
        reply.type = SERVER_ERR;
        strcpy(reply.msg, "员工不存在");
    }

    res = send(recvfd, &reply, sizeof(reply), 0);
    if (res < 0) {
        ERR("send error");
    }
}

void add_history(char *history_desc){
    // 添加历史记录
    sqlite3 *db = db_create();
    hsg_t history;
    strcpy(history.name, username);
    time_t timp = time(NULL);
    struct tm *tms =  localtime(&timp);
    sprintf(history.time, "%4d-%02d-%02d %02d:%02d:%02d", 
            tms->tm_year+1900, tms->tm_mon+1, tms->tm_mday, 
            tms->tm_hour, tms->tm_min, tms->tm_sec);
    strcpy(history.words, history_desc);
    insert_history(db, history);
    printf("添加历史记录\n");

}

